package com.synnex.mobile.servier.core.notification.service.impl;

import com.synnex.mobile.generic.core.pager.DataResults;
import com.synnex.mobile.generic.core.pager.Pagination;
import com.synnex.mobile.generic.core.util.ObjectUtil;
import com.synnex.mobile.servier.core.msgBoard.bo.MsgBo;
import com.synnex.mobile.servier.core.notification.bo.DeviceTokenBo;
import com.synnex.mobile.servier.core.notification.bo.NotificationBo;
import com.synnex.mobile.servier.core.notification.bo.criteria.NotificationCriteria;
import com.synnex.mobile.servier.core.notification.dao.DeviceTokenDao;
import com.synnex.mobile.servier.core.notification.dao.NotificationDao;
import com.synnex.mobile.servier.core.notification.dto.DeviceToken;
import com.synnex.mobile.servier.core.notification.dto.Notification;
import com.synnex.mobile.servier.core.notification.service.DeviceTokenService;
import com.synnex.mobile.servier.core.notification.service.NotificationService;
import com.synnex.mobile.servier.core.service.impl.AbstractServiceImpl;
import com.synnex.mobile.servier.web.utils.IOSPushMsgUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * To change this template use File | Settings | File Templates.
 */
@Transactional
public class NotificationServiceImpl extends AbstractServiceImpl implements NotificationService {

    protected NotificationDao notificationDao;
    protected DeviceTokenDao tokenDao;

    public void setTokenDao(DeviceTokenDao tokenDao) {
        this.tokenDao = tokenDao;
    }

    public void setNotificationDao(NotificationDao notificationDao) {
        this.notificationDao = notificationDao;
    }

//    public List<NotificationBo> getNotifications() {
//
//        List<Notification> notifications = notificationDao.getNotifications();
//
//        return NotificationBo.createListInstances(notifications);
//    }

    public DataResults<NotificationBo> getNotification(NotificationCriteria criteria, String from) {

        Pagination pagination = criteria.getPagination();
        List<Notification> notifications = notificationDao.getNotifications(pagination.getBegin(),pagination.getPageSize(),from);
        Integer count = notificationDao.countNotification(from);

        DataResults<NotificationBo> data = DataResults.create(count, NotificationBo.createListInstances(notifications), pagination);
        return data;


    }

    public Integer addNotification(NotificationBo bo) {

        Notification notification = ObjectUtil.copyProperties(bo, Notification.class);
        notification.setEntryDate(new Date());

        notificationDao.addNotification(notification);
        return notification.getId();
    }

    public void deleteNotify(Integer id) {
        notificationDao.deleteNotify(id);
    }

    public NotificationBo getNotificationBoById(Integer id) {
        Notification notification = notificationDao.getNotificationById(id);

        return NotificationBo.createInstance(notification);
    }


    public void sendNotification(){

        System.out.println("fdsafsafsa");
        Date nowDate = new Date();
//        Calendar now =Calendar.getInstance();
//        now.setTime(nowDate);

        Calendar old =Calendar.getInstance(Locale.CHINA);
        old.setTime(nowDate);
        old.add(Calendar.MINUTE, -10);

        Calendar future =Calendar.getInstance(Locale.CHINA);
        future.setTime(nowDate);
        future.add(Calendar.MINUTE,6);


        List<Notification> needSendNotify =   notificationDao.getNotifications(old.getTime(),future.getTime());

        if(needSendNotify!=null && !needSendNotify.isEmpty()){

            List<DeviceToken> tokenObjs = tokenDao.getTokens();
            List<String> tokens = new ArrayList<String>();
            if(null != tokenObjs && !tokenObjs.isEmpty()){
                for(DeviceToken token: tokenObjs){
                    tokens.add(token.getToken());
                }

            for(Notification notification : needSendNotify){

                StringBuilder    message = new StringBuilder("{'aps':{");
                message.append("'alert':'").append(notification.getTitle()).append("',")
                        .append("'icon':'").append(notification.getPicPath()).append("',")
                        .append("'id':'").append(notification.getId()) .append("'") ;
                message.append("}}");

                notification.setActualSendTime(new Date());
                notificationDao.updateNotification(notification);
                new SendNotifications(message.toString(),tokens,true).start();
            }


            }
        }

    }

    class SendNotifications extends Thread {

        private String msg;
        private List<String> tokens;
        private boolean isProd;

        public SendNotifications() {
        }
        public SendNotifications(String msg, List<String> tokens,boolean  isProd) {
            super();
            this.msg = msg;
            this.tokens = tokens;
            this.isProd = isProd;
        }

        public void run() {
            if(null != tokens && !tokens.isEmpty()){
                IOSPushMsgUtil iosPush = new IOSPushMsgUtil();
                tokens = iosPush.sendPush(tokens, "Certificates.servier.p12","synnex",msg,1,isProd);
//            tokens = iosPush.sendPush(tokens, "promocentralPNSCer_iPhone.p12","synnex",msg,1,true);
            }
        }
    }


}
